//
//  Navigation.swift
//  NavigationDemo
//
//  Created by hongyan.yang on 2022/5/10.
//
import SwiftUI

class Navigation: ObservableObject {
    @Published var secondViewShowing = false
}

struct NavigationToolBar: ViewModifier {
    var title: String
    var rightTitle: String
    var rightAction: ()->()
    
    init(_ title:String, _ rightTitle:String, _ rightAction:@escaping ()->()){
        self.title = title
        self.rightTitle = rightTitle
        self.rightAction = rightAction
    }
    
    func body(content: Content) -> some View {
        content
            .navigationBarBackButtonHidden(true)
            .navigationBarTitleDisplayMode(.inline)
            .toolbar(content: buildToolbar)
    }
    @ToolbarContentBuilder
    func buildToolbar() -> some ToolbarContent {
        ToolbarItem(placement: .principal) {
            Text(title)
                .font(.system(size: 17, weight: .bold, design: .default))
        }
        ToolbarItem(placement: .navigationBarTrailing) {
            Button(action: {
                rightAction()
            }) {
                Text(rightTitle)
                    .font(.system(size: 17, weight: .regular, design: .default))
            }
        }
    }
}
extension View {
    func navigationToolBar(_ title:String, _ rightTitle:String, _ rightAction:@escaping ()->()) -> some View {
        return modifier(NavigationToolBar(title, rightTitle, rightAction))
    }
}
